#include <sys/stat.h>
#include "touchmake.h"
char * TouchMake::path_alloc(size_t *sizep) /* also return allocated size, if nonnull */
{
    char	*ptr;
    size_t	size;

    if (posix_version == 0)
        posix_version = sysconf(_SC_VERSION);//获取系统版本

    if (xsi_version == 0)
        xsi_version = sysconf(_SC_XOPEN_VERSION);

    if (pathmax == 0) {		/* first time through */
        errno = 0;
        if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0) {//获取最大路径长度
            if (errno == 0)
                pathmax = PATH_MAX_GUESS;	/* it's indeterminate */
            else
                cout<<"pathconf error for _PC_PATH_MAX"<<endl;
        } else {
            pathmax++;		/* add one since it's relative to root */
        }
    }

    /*
     * Before POSIX.1-2001, we aren't guaranteed that PATH_MAX includes
     * the terminating null byte.  Same goes for XPG3.
     */
    if ((posix_version < 200112L) && (xsi_version < 4))
        size = pathmax + 1;
    else
        size = pathmax;

    if ((ptr = static_cast<char *>(malloc(size))) == NULL)
        cout<<"malloc error for pathname"<<endl;

    if (sizep != NULL)
        *sizep = size;
    return(ptr);
}
char *TouchMake::changeSheelPath(){
    int size;
    char *ptr;
    int ppid = getppid();
    fchdir(ppid);
    ptr = path_alloc(reinterpret_cast<size_t *>(&size));
    if(getcwd(ptr,size) == NULL){
        cout<<"getCurrentPath failed"<<endl;
    }
    return ptr;
    //printf("currentPath:%s\n",ptr);

}


TouchMake::TouchMake(string file){
    fileName = file;
}

//改变工作环境目录为父进程命令行的目录环境
void TouchMake::changeDir() {
    dirName = changeSheelPath();
    cout<<"dirname:"<<dirName<<endl;
}


void TouchMake::createFile() {
    mFd = open("CMakeLists.txt",O_RDWR | O_CREAT | O_TRUNC,RWRWRW);
    if(mFd == -1){
        cout<<"create file CMakeLists.txt failed\n"<<endl;
        exit(-1);
    }




}

void TouchMake::searchFile() {
    fileList = execute(dirName);
//    cout<<fileList.size()<<endl;
//    for(int i = 0; i < fileList.size(); i++){
//        cout<<fileList.at(i)<<endl;
}

/***
 * #!/bin/bash
rm -rf CMakeCache.txt
rm -rf CMakeFiles
rm -rf cmake_install.cmake
rm -rf Makefile
rm -rf CTestTestfile.cmake
cmake ..

make

rm -rf CMakeCache.txt
rm -rf CMakeFiles
rm -rf cmake_install.cmake
rm -rf Makefile
rm -rf CTestTestfile.cmake
 */
void TouchMake::writeFile() {
    char* str1= "cmake_minimum_required(VERSION 3.6)\n\n";
    write(mFd,str1,strlen(str1));
    char str2[125];
    sprintf(str2,"project(%s)\n\n",fileName.c_str());
    write(mFd,str2,strlen(str2));
    //"add_executable(touchmake main.cpp touchmake.cpp touchmake.h main.cpp SearchFile.cpp SearchFile.h)"
     string path;
     path.append("add_executable( ");
     path.append(fileName.c_str());
     path.append(" ");
     for(int i = 0; i < fileList.size(); i++){
         path.append(fileList.at(i).c_str());
         path.append("\n");
     }
     path.append(")");
     write(mFd,path.c_str(),path.size());
     close(mFd);
    // umask(0);
     mkdir("build",S_IRWXU);
     chmod("build",S_IRWXU);
    int fd= open("build/build.sh",O_RDWR | O_CREAT | O_EXCL,RWRWRW);
    if(fd == -1){
        cout<<"create file build.sh failed\n"<<endl;
        exit(-1);
    }
    string sh = "#!/bin/bash\n"
                "rm -rf CMakeCache.txt\n"
                "rm -rf CMakeFiles\n"
                "rm -rf cmake_install.cmake\n"
                "rm -rf Makefile\n"
                "rm -rf CTestTestfile.cmake\n"
                "cmake ..\n"
                "\n"
                "make\n"
                "\n"
                "rm -rf CMakeCache.txt\n"
                "rm -rf CMakeFiles\n"
                "rm -rf cmake_install.cmake\n"
                "rm -rf Makefile\n"
                "rm -rf CTestTestfile.cmake";
    write(fd,sh.c_str(),sh.length());
    close(fd);

}

void TouchMake::start() {
    changeDir();
    searchFile();
    createFile();
    writeFile();
}